package com.jungle.mvvm.sample.http;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;

import com.jungle.mvvm.http.DownLoadManager;
import com.jungle.mvvm.http.download.DownLoadSubscriber;
import com.jungle.mvvm.http.download.ProgressCallBack;
import com.jungle.mvvm.sample.api.UpdateApi;
import com.jungle.mvvm.sample.app.WanObserver;
import com.jungle.mvvm.sample.util.PathUtil;
import com.jungle.mvvm.update.DownLoadHelper;
import com.jungle.mvvm.update.MyDownLoadSubscriber;
import com.jungle.mvvm.update.UpdateBean;
import com.jungle.mvvm.utils.AppUtil;
import com.jungle.mvvm.utils.GsonUtil;
import com.xuexiang.xupdate.proxy.IUpdateHttpService;
import com.xuexiang.xupdate.utils.FileUtils;

import java.io.File;
import java.util.Map;
import okhttp3.ResponseBody;

/**
 * @ClassName OKHttpUpdateHttpService
 * @Description OKHttpUpdateHttpService
 * @Author Daniel
 * @Version V1.0.0
 */
public class OKHttpUpdateHttpService implements IUpdateHttpService {
    private String downloadId = "";

    @SuppressLint("CheckResult")
    @Override
    public void asyncGet(@NonNull String url, @NonNull Map<String, Object> params, @NonNull final Callback callBack) {
        UpdateNetwork.getService(UpdateApi.class)
                .Update()
                .compose(UpdateNetwork.applyService(null, new WanObserver<UpdateBean>() {
                    @Override
                    public void onWanSuccess(UpdateBean updateBean) {
                        if (updateBean.getVersionCode() > AppUtil.getAppVersionCode()) {
                            callBack.onSuccess(GsonUtil.toJson(updateBean));
                        }
                    }

                    @Override
                    public void onWanFailure(String e) {
                        callBack.onError(new Throwable(e));
                    }
                }));

    }

    @Override
    public void asyncPost(@NonNull String url, @NonNull Map<String, Object> params, @NonNull Callback callBack) {

    }

    @Override
    public void download(@NonNull String url, @NonNull String path, @NonNull final String fileName, @NonNull final DownloadCallback callback) {
        callback.onStart();
        ProgressCallBack<ResponseBody> progressCallBack = new ProgressCallBack<ResponseBody>(PathUtil.apkPath, fileName) {
            @Override
            public void onSuccess(ResponseBody responseBody) {
                File file = new File(PathUtil.apkPath + fileName);
                if (FileUtils.isFileExists(file)) {
                    callback.onSuccess(file);
                } else {
                    callback.onError(new Throwable());
                }
            }

            @Override
            public void progress(long progress, long total) {
                callback.onProgress((float) progress / (float) total, total);
            }

            @Override
            public void onError(Throwable e) {
                callback.onError(e);
            }
        };
        MyDownLoadSubscriber downLoadSubscriber = new MyDownLoadSubscriber<>(progressCallBack);
        DownLoadManager.getInstance().load(url, progressCallBack, downLoadSubscriber);
        downloadId = downLoadSubscriber.getDownloadId();
    }

    @Override
    public void cancelDownload(@NonNull String url) {
        DownLoadSubscriber downLoadSubscriber = DownLoadHelper.getInstance().getDownLoadSubscribers().get(downloadId);
        if (downLoadSubscriber != null) {

            downLoadSubscriber.dispose();
            DownLoadHelper.getInstance().getDownLoadSubscribers().remove(downloadId);
        }
    }
}
